/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.MrpAtp;

import java.sql.Timestamp;
import java.util.ArrayList;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import merlion.common.util.Consts;
import merlion.mrp.atpm.session.ATPSessionBeanLocal;
import merlion.mrp.ppm.entity.BackOrder;
import merlion.mrp.ppm.entity.IndexNDate;
import merlion.mrp.ppm.entity.IndexNValue;

/**
 *
 * @author Zhang Ying
 */
@ManagedBean
@ViewScoped
public class BackOrderManagerBean {
    public static final String VIEWED_BO = "ViewedBackOrder";

    @EJB
    private ATPSessionBeanLocal atpSession;
    private ArrayList<BackOrder> allBackOrders;
    private BackOrder selectedBO;
    private String boStrId;
    private BackOrder viewedBO;
    private ArrayList<Integer> counters;
    private ArrayList<Integer> counterForProduct;
    // present
    private ArrayList<Timestamp> showedDates;
    private ArrayList<ArrayList<Long>> showedQty;
    
    /** Creates a new instance of BackOrderManagerBean */
    public BackOrderManagerBean() {
    }

    public ArrayList<Integer> getCounters() {
        if (counters == null) {
            counters = new ArrayList<Integer>();
            for (int i = 0; i < getShowedDates().size(); i++) {
                counters.add(i);
            }
        }
        return counters;
    }

    public ArrayList<Integer> getCounterForProduct() {
        if (counterForProduct== null) {
            counterForProduct = new ArrayList<Integer>();
            for (int i = 0; i < 4; i++) {
                counterForProduct.add(i);
            }
        }
        return counterForProduct;
    }

    public ArrayList<BackOrder> getAllBackOrders() {
        if (allBackOrders == null) {
            allBackOrders = atpSession.getAllBackOrders();
        }
        return allBackOrders;
    }
    
    public String redirectToViewBackOrderDetail() {
        if (boStrId == null) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "Unsuccessful", "Please select back order."));
            return null;
        }
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove(VIEWED_BO);
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(VIEWED_BO, selectedBO);
        return getRedirectPath(Consts.VIEW_BACKORDER_PATH + "Detail");
    }

    public BackOrder getViewedBO() {
        if (viewedBO == null) {
            BackOrder bo = (BackOrder) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(VIEWED_BO);
            if (bo != null) {
                viewedBO = bo;
            }
        }
        return viewedBO;
    }

    public ArrayList<Timestamp> getShowedDates() {
        if (showedDates == null) {
            showedDates = new ArrayList<Timestamp>();
            int counter = 0; 
            for (IndexNDate ind : getViewedBO().getDates()) {
                if (renderBackOrderLine(counter)) {
                    showedDates.add(ind.getiDate());
                }
                counter ++;
            }
        }
        return showedDates;
    }

    public void setShowedDates(ArrayList<Timestamp> showedDates) {
        this.showedDates = showedDates;
    }

    public ArrayList<ArrayList<Long>> getShowedQty() {
        if (showedQty == null) {
            showedQty = new ArrayList<ArrayList<Long>>();
            for (int i = 0; i < 4;i ++) {
                ArrayList<Long> temp = new ArrayList<Long>();
                int counter = 0;
                for (IndexNValue inv : getViewedBO().returnBackOrderIndiv(i).getQuantities()) {
                    if (renderBackOrderLine(counter)) {
                        temp.add(inv.getLongValue());
                    }
                    counter++;
                }
                showedQty.add(temp);
            }
        }
        return showedQty;
    }

    public void setShowedQty(ArrayList<ArrayList<Long>> showedQty) {
        this.showedQty = showedQty;
    }

    public boolean renderBackOrderLine(Integer counter) {
        int render = 0;
        for (Integer c : counterForProduct) {
            if (viewedBO.returnBackOrderIndiv(c).getQuantities().get(counter).getLongValue() == 0) {
                render ++;
            }
        }
        if (render == 4) {
            return false;
        } else {
            return true;
        }
    }

    public void setViewedBO(BackOrder viewedBO) {
        this.viewedBO = viewedBO;
    }
    
    public String getBoStrId() {
        return boStrId;
    }

    public void setBoStrId(String boStrId) {
        this.boStrId = boStrId;
    }
    
    public BackOrder getSelectedBO() {
        return selectedBO;
    }

    public void setSelectedBO(BackOrder selectedBO) {
        this.selectedBO = selectedBO;
        this.setBoStrId(selectedBO.getBackOrderStrID());
    }
    
    private String getRedirectPath(String path) {
        return path + "?faces-redirect=true";
    }
    
    public String returnTimestampString(Timestamp t) {
        return Consts.sdfDmy.format(t);
    }
    
    public String returnProductAlpha(Integer i) {
        if (i.intValue() == 0) {
            //return Consts.ProductType.BAR_A.getText();
            return "A";
        } else if (i.intValue() == 1) {
            //return Consts.ProductType.BAR_B.getText();
            return "B";
        } else if (i.intValue() == 2) {
            //return Consts.ProductType.BAR_C.getText();
            return "C";
        } else if (i.intValue() == 3) {
            //return Consts.ProductType.BAR_D.getText();
            return "D";
        } else {
            return "";
        }
    }

    public String returnProductName(Integer i) {
        if (i.intValue() == 0) {
            return Consts.ProductType.BAR_A.getText();
        } else if (i.intValue() == 1) {
            return Consts.ProductType.BAR_B.getText();
        } else if (i.intValue() == 2) {
            return Consts.ProductType.BAR_C.getText();
        } else if (i.intValue() == 3) {
            return Consts.ProductType.BAR_D.getText();
        } else {
            return "";
        }
    }
}
